import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList; 
import oracle.jdbc.pool.OracleDataSource;
//getMarket displays the houses that are on the market in an html table
public class getMarket extends HttpServlet
{
	public static dbapi conn;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(htmlHeader.header);
		conn = new dbapi();
		//Connect to oracle
		conn.connect();
		if(!conn.connected)//if connection does not work, inform user w/ error message
		{
			out.println("SQL ERROR CANNOT CONNECT TO DATABASE!<br>");
			out.println(conn.msg + "<br>");
			return;
		}
		int ct = 0;
		
		ct = conn.getHomeCount();//sets the variable ct equal to the number of homes in the database using the homecount call
		if(ct == -1)//if ct = -1, print out "no houses in database"
		{
			out.println("No houses in database. <br>"+conn.msg);
			return;
		}
		//else
		//	out.println(ct+" houses in database.<br>");

		ArrayList<home> houseList = conn.getAllHomesOnMarket();//creates an array houseList w/ each of the homes on market
		
		if(houseList == null)//if the houseList array is null, display error message
		{
			out.println("Error: null resultset from database<br>"+conn.msg);
			return;
		}
		else//else do the following
		{
			//create html table
			out.println("<p style=\"font-size:20pt;\">Market</p>"+
					"<table border=1 width=100%><tr>"+
				    "<th width=15%>Address</th>"+
				    "<th width=20%>City</th>"+
				    "<th width=5%>State</th>"+
				    "<th width=10%>Class</th>"+
				    "<th width=10%>Agent</th>"+
					"<th width=10%>Price</th>"+
				    "<th width=10%>Date On Market</th>"+
				    "<th width=20%></th>"
				     );
			//for each house in the list, populate the table w/ the correct values
			for(int i=0;i<houseList.size();i++)
			{
				out.println("<tr>");
				out.println("<td>"+houseList.get(i).address+"</td>");
				out.println("<td>"+houseList.get(i).city+"</td>");
				out.println("<td>"+houseList.get(i).state+"</td>");
				out.println("<td>"+houseList.get(i).homeClass+"</td>");
				out.println("<td>"+houseList.get(i).agent+"</td>");
				out.println("<td>$"+houseList.get(i).price+"</td>");
				out.println("<td>"+houseList.get(i).dateOnMarket+"</td>");
				out.println("<td><a href=\"houseDetails?id="+Integer.toString(houseList.get(i).id)+"\">View House</a><br><a href=\"addAppliance?id="+Integer.toString(houseList.get(i).id)+"\">Add Appliance</a><br><a href=\"delHome?id="+Integer.toString(houseList.get(i).id)+"\">Delete Home</a></td>");
				out.println("</tr>");
			}
			out.println("</table>");
		}
	}
}
